package com.aiti.lulian.controller;


import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.ShowRecordAuditDto;
import com.aiti.lulian.handler.HeaderStyleHandler;
import com.aiti.lulian.service.ShowRecordAuditService;
import com.aiti.lulian.vo.CardBusinessContractExportVo;
import com.aiti.lulian.vo.ShowRecordAuditExportVo;
import com.aiti.lulian.vo.ShowRecordAuditVo;
import com.aiti.lulian.workflow.CompleteTaskRequest;
import com.aiti.lulian.workflow.ProcessInstance;
import com.aiti.lulian.workflow.RejectTaskRequest;
import com.aiti.lulian.workflow.WorkflowEngine;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/record/audit")
public class ShowRecordAuditController extends WebController {

    @Autowired
    private ShowRecordAuditService showRecordAuditService;



    @GetMapping("/page")
    public Message page(ShowRecordAuditDto dto) {
        String userId = findUserId();
        dto.setUserId(userId);
        Page page = this.startPage();
        List<ShowRecordAuditVo> list = showRecordAuditService.auditPage(page, dto);
        return this.Success(this.getPageResult(list, (int)page.getTotal()));
    }


    @GetMapping("/export")
    public void export(HttpServletResponse response, ShowRecordAuditDto dto) throws Exception {
        String userId = findUserId();
        dto.setUserId(userId);
        List<ShowRecordAuditExportVo> list = showRecordAuditService.auditList(dto);
        // 设置文件名，注意需要对中文进行 URL 编码
        String fileName = URLEncoder.encode("审批记录", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=\"" + fileName + ".xlsx\";" +
                        " filename*=UTF-8''" + fileName + ".xlsx");

        // 使用 EasyExcel 写入数据到输出流
        try (

                OutputStream outputStream = response.getOutputStream()
        ) {

            EasyExcel.write(outputStream, ShowRecordAuditExportVo.class)
                    .sheet("审批记录")
                    .registerWriteHandler(new HeaderStyleHandler())
                    .doWrite(list);
        }
    }







}
